package com.charging.controller.user;

import com.charging.entity.ChargingPile;
import com.charging.entity.ChargingRecord;
import com.charging.entity.User;
import com.charging.service.ChargingPileService;
import com.charging.service.RecordService;
import com.charging.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/record")
public class RecordController {


    @Resource
    private RecordService recordService;

    @Resource
    private UserService userService;

    @Resource
    private ChargingPileService chargingPileService;

    @GetMapping("/list/{id}")
    public String list(Model model, @PathVariable Long id){
        List<ChargingRecord> recordList = recordService.getAllRecords();
        model.addAttribute("recordList", recordList);

        User user = userService.getUserById(id);
        model.addAttribute("user",user);
        return "user/record/list";
    }

    // 新增页
    @GetMapping("/add/{id}")
    public String showAddForm(@PathVariable Long id, Model model ) {

        ChargingPile pile = chargingPileService.findById(id);
        model.addAttribute("chargingPile", pile);

        System.out.println("进入预约充电页面方法，充电桩ID：" + id);

        model.addAttribute("record", new ChargingRecord());
        return "user/record/add";
    }

    @PostMapping("/add")
    public String add(ChargingRecord record) {
        recordService.addRecord(record);
        return "admin/pile/list";
    }
}
